package com.ts.api.exp;

import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.entity.MsgFormat;

/**
 * Api消息异常
 */
public class ApiMsgException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private ApiEnum apiEnum;

    private MsgFormat msgFormat;

    private Exception e;

    private boolean receiveMode;

    private Object receiveData;

    public ApiMsgException() {
        super();
    }

    public ApiMsgException(ApiEnum apiEnum) {
        this(apiEnum.getMsg());
        this.apiEnum = apiEnum;
    }

    public ApiMsgException(ApiEnum apiEnum, Exception e) {
        this(apiEnum.getMsg());
        this.apiEnum = apiEnum;
        this.e = e;
    }

    public ApiMsgException(ApiEnum apiEnum, String msg) {
        this(msg);
        this.apiEnum = apiEnum;
    }

    public ApiMsgException(MsgFormat msgFormat) {
        this(msgFormat.getMsg());
        this.msgFormat = msgFormat;
    }

    public ApiMsgException(Exception e) {
        super(e);
    }

    public ApiMsgException(String msg) {
        super(msg);
    }

    public ApiMsgException(String msg, Throwable cause) {
        super(msg, cause);
    }

    public ApiEnum getApiEnum() {
        return apiEnum;
    }

    public MsgFormat getMsgFormat() {
        return msgFormat;
    }

    public Exception getException() {
        return this.e;
    }

    public boolean hasReceiveMode() {
        return receiveMode;
    }

    public void setReceiveMode(boolean receiveMode) {
        this.receiveMode = receiveMode;
    }

    public Object getReceiveData() {
        return receiveData;
    }

    public void setReceiveData(Object receiveData) {
        this.receiveData = receiveData;
    }

    /**
     *  获取最终ApiCode
     *  @return
     */
    public Integer getApiCode() {
        if (apiEnum == null) {
            return msgFormat.getCode();
        }
        return apiEnum.getCode();
    }

    /**
     *  获取最终ApiMsg
     *  @return
     */
    public String getApiMsg() {
        if (apiEnum == null) {
            return msgFormat.getMsg();
        }
        return apiEnum.getMsg();
    }
}
